﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using QuanLyHocPhi.Entity;
using QuanLyHocPhi.DAL;
using QuanLyHocPhi.Util;
namespace QuanLyHocPhi
{
    public partial class frmSinhvien : DevComponents.DotNetBar.Metro.MetroForm
    {
        SINH_VIEN_DAO svDao = new SINH_VIEN_DAO();
        HOA_DON_DAO hdDao = new HOA_DON_DAO();
        DOI_TUONG_DAO dtDao = new DOI_TUONG_DAO();
        MUC_PHI_DAO mpDao = new MUC_PHI_DAO();
        SINH_VIEN sv = new SINH_VIEN();
        DOI_TUONG dtuonghientai = new DOI_TUONG();
        MUC_PHI mp = new MUC_PHI();
        MUC_PHI mucphiHientai = new MUC_PHI();
        HOA_DON_CT_DAO hdctDao = new HOA_DON_CT_DAO();
        public frmSinhvien()
        {
            InitializeComponent();
        }



        private void btnThem_Click(object sender, EventArgs e)
        {
            btnThem.Text = "Lưu";
            int err = 0;
            if (txtTensv.Text == "")
            {
                errorInfo.SetError(txtTensv, "Yêu cầu nhập tên sinh viên");
                return;
            }
            
            SINH_VIEN sv = new SINH_VIEN();
            sv.MA_SO_SV = txtMasosv.Text;
            sv.NGAY_SINH = dtpNgaysinh.Value;
            sv.DIA_CHI = txtDiachi.Text;
            sv.SO_DIEN_THOAI = txtSodienthoai.Text;
            sv.TEN_SV = txtTensv.Text;
            sv.EMAIL = txtEMAIL.Text;
            HOA_DON hd = new HOA_DON();
            
            hd.DANG_KY_NHOM = KieuDuLieu.ChuyenSangsonguyen(cbbDangkynhom.SelectedText.ToString(), out err);
            hd.NGAY_KICH_HOAT = DateTime.Now;
            hd.GHI_CHU = "";

            hd.MA_LOAI_LOP = new DOI_TUONG() { MA_DOI_TUONG = ((DOI_TUONG)cbbLoailop.SelectedItem).MA_DOI_TUONG };
           
            if (cbbThoigiantt.SelectedItem == null)
            {
                MessageBox.Show("Chọn thời gian thanh toán", "Thông báo");

                return;
            }
          
            mucphiHientai = cbbThoigiantt.SelectedItem as MUC_PHI;          
            hd.MA_MUC_PHI = mucphiHientai;
            //1. Tao thong tin sv
            int newId = 0;
            if (svDao.Insert(sv, out newId))
            {
                //2. Tao hoa don (hop dong)
                int maHoaDon = 0;
                hd.MA_SV = newId;
                hdDao.Insert(hd,out maHoaDon);
                hd.MA_HOA_DON = maHoaDon;
                //3. tao chi tiet hoa don, bao gom lich trinh dong hoc phi, chen vao bang HOA_DON_CT
            
                //4. Cap nhat giao dien: Them 1 dong vao  bang
                
                txtMasosv.Text = "";
                txtTensv.Text = "";
                cbGioitinh.Checked = false;
                txtDiachi.Text = "";
                txtSodienthoai.Text = "";
                txtEMAIL.Text = "";
                cbbLoailop.SelectedItem = "";
                cbbThoigiantt.SelectedItem = "";
                cbbDangkynhom.SelectedItem ="";

                  //sinh cac truong chi tiet hoa don (cac lan thu phi), su dung stored procedure
                    HOA_DON_CT hdct = new HOA_DON_CT();                 
                    hdct.MA_HOA_DON = hd;                  
                    hdct.NGAY_THU_PHI = DateTime.Now;
                    hdct.TRANG_THAI = false;

                    hdctDao.GenSubInvoices(hdct);

                    frmSinhvien_Load(sender, e);
                    
            }

        }



        private void btnTimkiem_Click(object sender, EventArgs e)
        {
           
               
        }
        IList<SINH_VIEN> ds = null;
        IList<DOI_TUONG> dtuong = null;
        IList<HOA_DON> hdon = null;
        HOA_DON_DAO hd = new HOA_DON_DAO();
        private void frmSinhvien_Load(object sender, EventArgs e)
        {
          dtuong = dtDao.GetObjectsByLOP("class_type");
          foreach (DOI_TUONG dt in dtuong)
            {
                cbbLoailop.Items.Add(dt);
            }
            //NAP DU LIEU LEN BANG
            ds = svDao.GetStudentsByNameOrCode("%");
            dataGridViewX1.AutoGenerateColumns = false;
            dataGridViewX1.DataSource = ds;
            
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            
            
            sv.MA_SO_SV = txtMasosv.Text;
            sv.NGAY_SINH = dtpNgaysinh.Value;
            sv. DIA_CHI = txtDiachi.Text;
            sv.SO_DIEN_THOAI = txtSodienthoai.Text;
            sv.TEN_SV = txtTensv.Text;
            sv.EMAIL = txtEMAIL.Text;

            //HOA_DON hd = new HOA_DON();
            
            
           // hd.MA_LOAI_LOP = new DOI_TUONG() { MA_DOI_TUONG = ((DOI_TUONG)cbbLoailop.SelectedItem).MA_DOI_TUONG };
           // hd.MA_MUC_PHI = mpDao.GetFeeLevelById(hd.MA_LOAI_LOP.MA_DOI_TUONG);

            svDao.Update(sv);
            frmSinhvien_Load(sender, e);
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            
            sv.MA_SO_SV = txtMasosv.Text;
            sv.NGAY_SINH = dtpNgaysinh.Value;
            sv.DIA_CHI = txtDiachi.Text;
            sv.SO_DIEN_THOAI = txtSodienthoai.Text;
            sv.TEN_SV = txtTensv.Text;
            sv.EMAIL = txtEMAIL.Text;

            svDao.Delete(sv.MA_SV);

            txtMasosv.Text = "";
            txtTensv.Text = "";
            txtDiachi.Text = "";
            txtSodienthoai.Text = "";
            txtEMAIL.Text = "";
            frmSinhvien_Load(sender, e);

           

            
            //HOA_DON hd = new HOA_DON();
            
            
           // hd.MA_LOAI_LOP = new DOI_TUONG() { MA_DOI_TUONG = ((DOI_TUONG)cbbLoailop.SelectedItem).MA_DOI_TUONG };
           // hd.MA_MUC_PHI = mpDao.GetFeeLevelById(hd.MA_LOAI_LOP.MA_DOI_TUONG);
            
           
        }
        
        private void dataGridViewX1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            
          //1: clich banh -> lay tra sinh vien trong danh sach da nap len bang -> SinhVienHienTai
            txtMasosv.Text = ds[e.RowIndex].MA_SO_SV;
            txtTensv.Text = ds[e.RowIndex].TEN_SV;
            txtDiachi.Text = ds[e.RowIndex].DIA_CHI;
            txtSodienthoai.Text = ds[e.RowIndex].SO_DIEN_THOAI;
            txtEMAIL.Text = ds[e.RowIndex].EMAIL;
            dtpNgaysinh.Value = ds[e.RowIndex].NGAY_SINH;

            sv = ds[e.RowIndex];
            //2. Dung 1 doi tuong của Hoa_Don_DAo -> tim trong bang Hoa_Don co hoa doan nao của sinh vien co MA_SV nhu sinh viên trên ko --> HoaDonHienTai

            //3. Trong HoaDonHienTai (kieu HOA_DON) em se co cac truong MA_LOAI_LOP, MA_LOP, MA_MUC_PHI
            //tư do em dung cac DAO tuong ưng: DOI_TUONG_DAO, LOP_HOC_DAO, MUC_PHI_DAO tim cac doi tuong co ID (ma) nhu o trong hoa don
            //--> em co cac doi tuong


            //4. Hien thi du lieu tu cac doi tuong ra o van ban, combobox. Xong
            


        }

        private void dataGridViewX1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            //txtMasosv.Text = ds[e.RowIndex].MA_SO_SV;
            //txtTensv.Text = ds[e.RowIndex].TEN_SV;
            //txtDiachi.Text = ds[e.RowIndex].DIA_CHI;
            //txtSodienthoai.Text = ds[e.RowIndex].SO_DIEN_THOAI;
            //txtEMAIL.Text = ds[e.RowIndex].EMAIL;
            //cbbLoailop.SelectedItem = dtuong[e.RowIndex].TEN_DOI_TUONG;
            //cbGioitinh.Checked = ds[e.RowIndex].GIOI_TINH;
            //dtpNgaysinh.Value = ds[e.RowIndex].NGAY_SINH;
            //cbbDangkynhom.SelectedItem = hdon[e.RowIndex].DANG_KY_NHOM;
        }

       

        private void cbbThoigiantt_SelectedIndexChanged(object sender, EventArgs e)
        {
            
        }

        private void cbbLoailop_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbbLoailop.SelectedItem != null)
            {
                dtuonghientai = cbbLoailop.SelectedItem as DOI_TUONG;
                cbbThoigiantt.ValueMember = "SO_LAN_THANH_TOAN";
                cbbThoigiantt.DisplayMember = "SO_LAN_THANH_TOAN";
                cbbThoigiantt.DataSource = new BindingSource(mpDao.SelectByObjectId(dtuonghientai.MA_DOI_TUONG), null);
                // cbbThoigiantt.
            }
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
